//
//  DetailCellHelper2.m
//  JZMCFM
//
//  Created by Mc on 15/11/2.
//  Copyright (c) 2015年 陈卓. All rights reserved.
//

#import "DetailCellHelper2.h"
static DetailCellHelper2 *manager = nil;
@implementation DetailCellHelper2

+ (instancetype)sharedInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[DetailCellHelper2 alloc] init];
    });
    return manager;
}

- (void)downLoadWithUrlString:(NSString *)urlString withResultBlock:(ResultBlock)resultBlock
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
      [SVProgressHUD showWithStatus:@"加载数据" maskType:SVProgressHUDMaskTypeBlack];
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.requestSerializer = [AFJSONRequestSerializer serializer];
        [manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSMutableArray *listArray = responseObject[@"list"];
            [self.cellArray removeAllObjects];
            for (NSDictionary *dict in listArray) {
                DetailModel2 *p = [[DetailModel2 alloc] init];
                [p setValuesForKeysWithDictionary:dict];
                [self.cellArray addObject:p];
            }
            NSLog(@"%@",self.cellArray);
            dispatch_async(dispatch_get_main_queue(), ^{
                resultBlock();
            });
            [SVProgressHUD showSuccessWithStatus:@"加载成功" duration:1.0];
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            
            NSLog(@"-------------%@----------",error);
        }];
        
        
        
//        NSURL *url = [NSURL URLWithString:urlString];
//        NSURLRequest *request = [NSURLRequest requestWithURL:url];
//        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//            NSMutableDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//            NSMutableArray *listArray = result[@"list"];
//            [self.cellArray removeAllObjects];
//            for (NSDictionary *dict in listArray) {
//                DetailModel2 *p = [[DetailModel2 alloc] init];
//                [p setValuesForKeysWithDictionary:dict];
//                [self.cellArray addObject:p];
//            }
//            
//            dispatch_async(dispatch_get_main_queue(), ^{
//                resultBlock();
//            });
//
//        }];
        
    });
}



- (NSMutableArray *)cellArray
{
    if (!_cellArray) {
        _cellArray = [NSMutableArray array];
    }
    return _cellArray;
}

@end
